﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orion.Models;
using Orion.MyLib;

namespace Orion.Controllers
{
	public class SearchController : Controller
	{
		NewsArticleRepositry m_newsArticleRepository = new NewsArticleRepositry();

		public ActionResult Index( )
		{
			return View( );
		}

		public ActionResult Search( )
		{
			return View( );
		}

		[HttpPost]
		public ActionResult SearchResult( string searchString )  //http://ericdotnet.wordpress.com/2009/04/09/jquery-search-box-and-aspnet-mvc/
		{
			try
			{
				//validate
				if (string.IsNullOrEmpty( searchString ) )
				{
					return View("NotFound");
				}

				//array for every character that seperate words populated
				char[] checkChars = { ' ', ',', '.', ':', '\t', };

				//search sentence split into words
				string[] words = searchString.Split(checkChars);

				List<IQueryable<NewsArticle>> newsArticleResult = new List<IQueryable<NewsArticle>>( );

				//searched for each word in search sentence
				foreach (var w in words)
				{
					newsArticleResult.Add(m_newsArticleRepository.GetAllNewsByName( w ) );
				}

				return View( newsArticleResult );
			}
			catch ( Exception ex )
			{
				var log = new Logger( );
				log.LogExcepion( ex );
				return View( "Error" );
			}
		}
	}
}	
